<template>
  <div v-loading="delLoading">
    <Search :search="search" :reset="reset" :inline="true">
      <template v-slot:body>
        <el-form-item label="销售退货单" prop="returnnumber" maxlength="5">
          <el-input v-model="query.returnnumber" name="returnnumber" clearable />
        </el-form-item>

        <el-form-item label="备注" prop="remark" maxlength="5">
          <el-input v-model="query.remark" name="remark" clearable />
        </el-form-item>

        <el-form-item label="状态" prop="state">
          <el-select v-model="query.state" placeholder="请选择" clearable>
            <el-option label="未审核" value="0" />

            <el-option label="审核" value="1" />
          </el-select>
        </el-form-item>

        <!-- <el-form-item label="回写状态" prop="backstate">
                  <el-select v-model="query.backstate" placeholder="请选择" clearable>
                    <el-option label="未回写" value="0" />
                    <el-option label="回写成功" value="1" />
                    <el-option label="回写失败" value="2" />
                  </el-select>
                </el-form-item> -->
      </template>
    </Search>

    <div class="table-default">
      <el-table :data="tableData"  v-loading="loading" border>
        <el-table-column prop="returnnumber" label="销售退货单" width="250"/>

        <el-table-column prop="remark" label="备注" />

        <el-table-column prop="writestate" label="回写状态" >
          <template #default="scope">
            <el-tag class="ml-2" type="info" v-if="scope.row.writestate == null || scope.row.writestate == ''">无数据</el-tag>

            <el-tag class="ml-2" type="warning" v-else>{{ scope.row.writestate }}</el-tag>

            <!-- <el-tag class="ml-2" type="success" v-else>{{ scope.row.writestate }}</el-tag> -->
          </template>
        </el-table-column>

        <el-table-column prop="writeback" label="回写备注" />

        <el-table-column prop="creator_id" label="创建者"  />

        <el-table-column prop="created_at" label="退货时间" width="150" />

        <el-table-column prop="updated_at" label="更新时间" width="150" />

        <el-table-column label="操作" fixed="right" width="200">
          <template #default="scope">
            <!-- {{JSON.stringify(scope.row)}} -->

            <div class="btn-box">
              <router-link :to="{ path: '/Salesmanagement/salesreturnDetails', query: { id: `${scope.row.id}`, state: `${scope.row.state}` } }">
                <Show />
              </router-link>
              <el-button type="warning" size="small" @click="approveHandler(scope.row.id)" v-if="scope.row.state != '已审核'" v-loading.fullscreen.lock="fullscreenLoading">审核</el-button>
              <Destroy @click="destroy(api, scope.row.id)" style="margin-left: 0" v-if="scope.row.state != '已审核'" />
            </div>
          </template>
        </el-table-column>
      </el-table>
      <Paginate />

      <Dialog v-model="visible" :title="title" destroy-on-close>
        <Create @close="close(reset)" :primary="id" :api="api" />
      </Dialog>
    </div>
  </div>
</template>



<script lang="ts" setup>
import { computed, onMounted, toRaw, ref, nextTick } from 'vue'

import { useRouter } from 'vue-router'
import { useGetList } from '/admin/composables/curd/useGetList'
import { useDestroy } from '/admin/composables/curd/useDestroy'
import { useOpen } from '/admin/composables/curd/useOpen'
import http from '/admin/support/http'
import { ElMessage } from 'element-plus'
import { setTableHeight } from '/admin/utils/setHeight.js'

let router = useRouter()
const api = 'salesmanagement/salesreturn'
const { data, query, search, reset, loading, toraw } = useGetList(api)
const { destroy, deleted,delLoading } = useDestroy()
const { open, close, title, visible, id } = useOpen()
const tableData = computed(() => data.value?.data)
console.log(tableData)
const writestateFormatter = {
  0: '回写失败',
  1: '回写成功',
}
const fullscreenLoading = ref(false)
const approveHandler = id => {
  fullscreenLoading.value = true
  http
    .put(`/salesmanagement/interfaceaudit/${id}`)
    .then(r => {
      fullscreenLoading.value = false
      ElMessage({
        message: r.data.message,
        type: 'success',
      })
      search()
    })

    .catch(error => {
      fullscreenLoading.value = false
    })
}

onMounted(() => {
  console.log('触发了onmounted')
  search()
  deleted(reset)
  setTableHeight('.table-default')
})
</script>

<style>
.btn-box {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
}
.el-table {
  height: 87%;
}
</style>
